/*
 * 2007/05
 * 2007/10
 * 2011/11/17 - 13:32
 */
package org.nettyflux.example.asteroids;

import java.awt.Color;
import java.awt.Graphics2D;

/**
 * @author Victor Williams Stafusa da Silva
 */
public class Paddle implements ScreenObject {

    private static final double SPEED = 100.0;

    public static final int HEIGHT = 70;
    public static final int WIDTH = 10;

    private final RenderScreen screen;
    private volatile boolean goingUp;
    private volatile boolean goingDown;
    private double x;
    private double y;
 
    public Paddle(int x, int y, RenderScreen screen) {
        this.x = x;
        this.y = y;
        this.screen = screen;
    }

    @Override
    public void move(double deltaT) {
        if (goingUp == goingDown) return;

        int minY = 0;
        int maxY = screen.getRenderHeight() - HEIGHT;
        if (goingUp) y -= SPEED * deltaT;
        else if (goingDown) y += SPEED * deltaT;
        if (y > maxY) y = maxY;
        if (y < minY) y = minY;
    }

    @Override
    public void draw(Graphics2D g) {
        g.setColor(Color.RED);
        g.fillRect(round(x), round(y), WIDTH, HEIGHT);
    }

    public void setGoingUpKey(boolean pressed) {
        this.goingUp = pressed;
    }

    public void setGoingDownKey(boolean pressed) {
        this.goingDown = pressed;
    }

    public double getX1() {
        return x;
    }

    public double getY1() {
        return y;
    }

    public double getX2() {
        return x + WIDTH;
    }

    public double getY2() {
        return y + HEIGHT;
    }

    public double getHeight() {
        return HEIGHT;
    }

    public double getWidth() {
        return WIDTH;
    }

    private static int round(double v) {
        return (int) Math.floor(v + 0.5);
    }
}